package org.hhchat.leetcode.tree;

import org.hhchat.leetcode.dataStruct.TreeNode;

import java.util.LinkedList;
import java.util.Queue;

/**
 * Created by hh23485 on 2017/7/3.
 */
public class code499 {
    public static  class Codec {




        // Encodes a tree to a single string.
        public String serialize(TreeNode root) {
            Queue<TreeNode> queue = new LinkedList<>();
            StringBuilder stringBuilder = new StringBuilder();

            queue.add(root);
            while (!queue.isEmpty()){
                root = queue.poll();
                if(root!=null){
                    stringBuilder.append(root.val).append(",");
                    queue.add(root.left);
                    queue.add(root.right);
                }else{
                    stringBuilder.append(0).append(",");
                }
            }
            return stringBuilder.toString();
        }

//         Decodes your encoded data to tree.
        public TreeNode deserialize(String data) {
            Queue<TreeNode> queue = new LinkedList<>();
            String []datas = data.split(",");
            if(datas.length==1){
                return null;
            }
            TreeNode root = new TreeNode(data.charAt(0) - '0');
            queue.add(root);
            int index=1;
            while (index < datas.length) {
                TreeNode v = queue.poll();
                if (v == null) {
                    continue;
                }
                if(datas[index].equals("0")){
                    v.left = null;
                }else{
                    v.left = new TreeNode(Integer.valueOf(datas[index]));
                }
                index++;
                if(datas[index].equals("0")){
                    v.right = null;
                }else{
                    v.right = new TreeNode(Integer.valueOf(datas[index]));
                }
                index++;
                queue.add(v.left);
                queue.add(v.right);
            }
            return root;
        }

//        public void deserializeHelper(String data, int index, TreeNode root) {
//            int right = index * 2+1;
//            int right = index * 2 + 2;
//            if (data.charAt(left) != '0') {
//                root.left = new TreeNode(data.charAt(left) - '0');
//                deserializeHelper(data, left, root.left);
//            } else {
//                root.left = null;
//            }
//
//            if (data.charAt(right) != '0') {
//                root.right = new TreeNode(data.charAt(right) - '0');
//                deserializeHelper(data, right, root.right);
//            } else {
//                root.right = null;
//            }
//
//        }
    }
    public static void main(String[] args){
        Codec codec = new Codec();
        TreeNode treeNode = new TreeNode(2);
        treeNode.left = null;
        treeNode.right = new TreeNode(3);

        String data = codec.serialize(treeNode);
        System.out.println(data);

        TreeNode treeNode1 = codec.deserialize(data);
        System.out.println(treeNode1.val);
        System.out.println(treeNode1.left);
        System.out.println(treeNode1.right);

    }
}
